package com.lotus.lqmp.service.common.config;

import com.lotus.lqmp.service.common.filters.XXXWebFilter;
import com.lotus.lqmp.service.common.interceptor.XXXInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * web相关配置
 * @author Liwei.Zhang3
 * @since 2022/01/14 15:35
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    // 最大连接数20
    @Value("${remote.maxTotalConnect:20}")
    private int maxTotalConnect;

    // 单路由最大连接数10
    @Value("${remote.maxConnectPerRoute:10}")
    private int maxConnectPerRoute;

    // 连接超时3s
    @Value("${remote.connectTimeout:3000}")
    private int connectTimeout;

    // 读超时10s
    @Value("${remote.readTimeout:10000}")
    private int readTimeout;

    // 连接不够用的等待时间5s
    @Value("${remote.connectionRequestTimeout:5000}")
    private int connectionRequestTimeout;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new XXXInterceptor()).addPathPatterns("/**");
    }

    @Bean
    public FilterRegistrationBean registrationFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new XXXWebFilter());
        registration.addUrlPatterns("/**");
        registration.setName("LogCostFilter");
        registration.setOrder(1);
        return registration;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        WebMvcConfigurer.super.configureMessageConverters(converters);
        resetConverters(converters);
    }

    @Bean
    @ConditionalOnMissingBean(RestTemplate.class)
    public RestTemplate restTemplate() {
        // 创建连接管理器 默认支持https http
        PoolingHttpClientConnectionManager pm = new PoolingHttpClientConnectionManager();
        pm.setMaxTotal(maxTotalConnect);
        pm.setDefaultMaxPerRoute(maxConnectPerRoute);
        //创建httpClient
        HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(pm).build();
        //创建HttpComponentsClientHttpRequestFactory
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        requestFactory.setConnectTimeout(connectTimeout);
        requestFactory.setReadTimeout(readTimeout);
        requestFactory.setConnectionRequestTimeout(connectionRequestTimeout);

        RestTemplate restTemplate = new RestTemplate(requestFactory);
        //用UTF-8 StringHttpMessageConverter替换默认StringHttpMessageConverter，解决请求回来的数据乱码问题
        List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
        resetConverters(converters);
        return restTemplate;
    }

    private void resetConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(converter -> converter instanceof StringHttpMessageConverter);
        converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
    }
}
